import {Injectable} from "@angular/core";
import { HttpClient,HttpParams } from '@angular/common/http';
import {toPromise} from "rxjs-compat/operator/toPromise";

@Injectable()
export class TrainingService {
  trainingServeUrl = 'https://creativitytrainingserver.lessonplan.cn';//创造力相关接口地址
  paperGuid = '0637bb90-2654-4336-80ae-a47015ddc307';//威廉斯创造试卷ID
  serveUrl='https://clientaccountserver.lessonplan.cn'; /*学生用户API地址*/
  constructor(
    private http: HttpClient,
  ) { }
  //获取训练题目
  getQuestionByPaperId():Promise<any>{
    return this.http.get(this.trainingServeUrl+'/question',{
      params: new HttpParams().set('id', this.paperGuid)
    }).toPromise().then(res=>{
       return res;
    })
  }
  //创建答题卡
  createCard(trainingID,userID):Promise<any>{
    let data = {FK_TrainingGuid:trainingID,FK_UserGuid:userID};
    return this.http.post(this.trainingServeUrl+'/anwser',data).toPromise().then(res=>{
      return res;
    })
  }
  //根据用户姓名创建答题卡
  createCardByName(trainingID,userName):Promise<any>{
    let data = {FK_TrainingGuid:trainingID,userName:userName};
    return this.http.post(this.trainingServeUrl+'/anwser',data).toPromise().then(res=>{
      return res;
    })
  }
  /*根据训练ID和用户ID来获取答题卡*/
  getCard(trainingID,userID):Promise<any>{
    return this.http.get(this.trainingServeUrl+'/anwser/paper',{
      params: new HttpParams().set('trainingID', trainingID).set('userID',userID)
    }).toPromise().then(res=>{
      return res;
    })
  }
/*更新答题卡*/
updateCardByID(cardID,content,score):Promise<any>{
  let Content = JSON.stringify(content);
  let Score = JSON.stringify(score);
  let data = {id:cardID,Content:Content,Score:Score};
  return this.http.put(this.trainingServeUrl+'/anwser',data).toPromise().then(res=>{
    return res;
  })
}
/*获取答题卡根据答题卡ID*/
getAnswerCardById(answerCardId):Promise<any>{
  return this.http.get(this.trainingServeUrl+'/anwser',{
    params: new HttpParams().set('id', answerCardId)
  }).toPromise().then(res=>{
    return res;
  })
}
/*更新答题卡完成状态*/
updateFinishById(answerCardId):Promise<any>{
  return this.http.get(this.trainingServeUrl+'/anwser/finish',{
    params: new HttpParams().set('id', answerCardId)
  }).toPromise().then(res=>{
    return res;
  })
}
  /*根据训练Guid获取训练*/
  getTrainingByID(trainingID):Promise<any>{
    return this.http.get(this.trainingServeUrl+'/training/id',{
      params:new HttpParams().set('id',trainingID)
    }).toPromise().then(res=>{
      return res;
    })
  }
  /*根据学生的guid获取学生加入的班级的Guid*/
  getJoinClass (stuGuid):Promise<any> {
    return this.http.get(this.serveUrl+'/user/joined/'+stuGuid).toPromise().then(res=>{
      return res;
    })
  }
}



